#include "lh_port.h"

volatile LH_U8 LCD_OperateMut = 0;    


LH_U16 Touch_ReadX(void)
{ 
    LH_U16 Data;
    
    Data = XPT2046_ReadY();
    
    return Data;
}



void LCD_WR_Data(LH_U16 data)
{

}



LH_U16 Touch_ReadY(void)
{
    LH_U16 Data;

    Data = XPT2046_ReadX();
    
    return Data;
}


LH_U16 TFT_GetPoint(LH_U16 x, LH_U16 y)
{
    LH_U16 Data;
    
    LCD_OperateMut = 1;
    Data = RA8875_GetPoint(x, y);
    LCD_OperateMut = 0;
    
    return Data;
}


void TFT_SetPoint(LH_U16 x,LH_U16 y,LH_U16 color)
{
    LCD_OperateMut = 1;
    RA8875_SetPoint(x, y, color);
    LCD_OperateMut = 0;
}



void TFT_SetWindows(LH_U16 sx, LH_U16 sy, LH_U16 ex, LH_U16 ey)
{
    LCD_OperateMut = 1;
    RA8875_SetDispWin(sx, sy, ex, ey);
    LCD_OperateMut = 0;
}



void TFT_SetCursor(LH_U16 x,LH_U16 y)
{
    LCD_OperateMut = 1;
    RA8875_SetCursor(x, y);
    LCD_OperateMut = 0;
}



void TFT_FillRect(LH_U16 sx, LH_U16 sy, LH_U16 ex, LH_U16 ey, LH_U16 color)
{
    LCD_OperateMut = 1;
    RA8875_FillRect(sx, sy, ex, ey, color);
    LCD_OperateMut = 0;
}



LH_U8 LH_ShowStrAt(LH_S16 sx, LH_S16 sy, LH_C8 *str, LH_U16 color)
{
    FATFS   Fs;             

    FIL     File;           
    DIR     Dir;            

    UINT    ByteR;          
    WORD    Count1;         
	WORD    Count2;         
    WORD    Count3;         
    UCHAR   AsisReg1;       
	
	UCHAR   Hzh,Hzl;			  
	DWORD   Offset; 
   
    CPU_IntDis();
    f_mount(0, &Fs);        
    if(f_opendir(&Dir, "") == FR_OK)            
        if(f_open(&File, "FONT/936.DZK", FA_OPEN_EXISTING | FA_READ) == FR_OK)	
        {
            CPU_IntEn();
            if(CheckBuffSta() == LH_BUFF_FREE)  
            {
                SetBuffBusy();                  
                Count3 = 0;
                do
                {
                    Hzh  = *str++;
                    if(Hzh <= 0x80)
                    {
                        TFT_PutChar(sx+Count3*8, sy, Hzh, color);
                        Count3 ++;
                    }
                    else
                    {
                        Hzl  = *str++; 
                        
                        if(Hzh<0x81 || Hzl<0x40 || Hzl==0xff || Hzh==0xff)  
                        {   		    
                            for(Count1 = 0; Count1 < 32; Count1 ++)
                                LH_DataBuff[Count1] = 0;
                        }
                        else   
                        {     
                            Hzh -= 0x81;
                            if (Hzl < 0x7f)  Hzl -= 0x40;           
                            else             Hzl -= 0x41;
                            Offset = ((DWORD)190 * Hzh + Hzl)*(32); 
                            
                            CPU_IntDis();
                            f_lseek(&File, Offset); 
                            if(f_read(&File, LH_DataBuff, 32, &ByteR) != FR_OK)
                                break; 
                            CPU_IntEn();
                        }

                        
                        for(Count1 = 0; Count1 < 16; Count1 ++)
                            for(Count2 = 0; Count2 < 16; Count2 ++)
                                {
                                    AsisReg1 = LH_DataBuff[Count1*2+(Count2/8)];
                                    
                                    if( (AsisReg1 & (0x80 >> (Count2%8))) != 0 )
                                        TFT_SetPoint(sx+Count2+Count3*8, sy+Count1, color);
                                    else
                                        if(CheckTftPutBack())
                                            TFT_SetPoint(sx+Count2+Count3*8, sy+Count1, GetBackColor());
                                } 
                                
                        Count3 += 2;
                    }
                }
                while(*str != 0);
                SetBuffFree();          
            }
            CPU_IntDis();
            f_close(&File); 
       }
    f_mount(0,NULL);        
    CPU_IntEn();
       
    return LH_OK;
}



LH_U8 LH_ShowStrRg(LH_S16 sx, LH_S16 sy, LH_S16 ex, LH_S16 ey, LH_C8 *str, LH_U16 color)
{
    FATFS   Fs;             

    FIL     File;           
    DIR     Dir;            

    UINT    ByteR;          
    WORD    Count1;         
	WORD    Count2;         
    WORD    Count3;         
    WORD    Count4;         
    UCHAR   AsisReg1;       
	
	UCHAR   Hzh,Hzl;			  
	DWORD   Offset; 
   
    CPU_IntDis();
    f_mount(0, &Fs);        
    if(f_opendir(&Dir, "") == FR_OK)            
        if(f_open(&File, "FONT/936.DZK", FA_OPEN_EXISTING | FA_READ) == FR_OK)	
        {
            CPU_IntEn();
            if(CheckBuffSta() == LH_BUFF_FREE)  
            {
                SetBuffBusy();                  
                Count3 = 0;
                Count4 = 0;
                do
                {
                    Hzh  = *str++;
                    
                    if (Hzh == '\n')
                    {
                        Count3 = 0;
                        Count4 += 16;
                        if(sy+Count4 > ey)
                            break;  
                    }
                    else if (Hzh == '\t')
                    {
                        Count3 += 32;
                        if(sx+Count3+8 > ex)
                        {
                            Count3 = 0;
                            Count4 += 16;
                            if(sy+Count4 > ey)
                                break;
                        }
                    }
                    else if (Hzh <= 0x80)
                    {
                        if(sx+Count3+8 > ex)
                        {
                            Count3 = 0;
                            Count4 += 16;
                            if(sy+Count4 > ey)
                                break;
                        }
                        TFT_PutChar(sx+Count3, sy + Count4, Hzh, color);
                        Count3 += 8;
                    }
                    else
                    {
                        Hzl  = *str++; 
                        
                        if(Hzh<0x81 || Hzl<0x40 || Hzl==0xff || Hzh==0xff)  
                        {   		    
                            for(Count1 = 0; Count1 < 32; Count1 ++)
                                LH_DataBuff[Count1] = 0;
                        }
                        else   
                        {     
                            Hzh -= 0x81;
                            if (Hzl < 0x7f)  Hzl -= 0x40;           
                            else             Hzl -= 0x41;
                            Offset = ((DWORD)190 * Hzh + Hzl)*(32); 
                            
                            CPU_IntDis();
                            f_lseek(&File, Offset); 
                            if(f_read(&File, LH_DataBuff, 32, &ByteR) != FR_OK)
                                break; 
                            CPU_IntEn();
                        }

                        if(sx+Count3+16 > ex)
                        {
                            Count3 = 0;
                            Count4 += 16;
                            if(sy+Count4 > ey)
                                break;
                        }
                        
                        for(Count1 = 0; Count1 < 16; Count1 ++)
                            for(Count2 = 0; Count2 < 16; Count2 ++)
                                {
                                    AsisReg1 = LH_DataBuff[Count1*2+(Count2/8)];
                                    
                                    if( (AsisReg1 & (0x80 >> (Count2%8))) != 0 )
                                        TFT_SetPoint(sx+Count2+Count3, sy+Count1+Count4, color);
                                    else
                                        if(CheckTftPutBack())
                                            TFT_SetPoint(sx+Count2+Count3, sy+Count1+Count4, GetBackColor());
                                } 
                                
                        Count3 += 16;
                    }
                }
                while(*str != 0);
                SetBuffFree();          
            }
            CPU_IntDis();
            f_close(&File); 
       }
    f_mount(0,NULL);        
    CPU_IntEn();
       
    return LH_OK;
}



LH_U8 LH_ShowBmpAt(LH_S16 sx, LH_S16 sy, LH_C8 *filename)
{
    FATFS   Fs;             

    FIL     File;           
    DIR     Dir;            
    UINT    ByteR;          
    WORD    Read_Flag = 0;  
    WORD    Count1;         
    WORD    AsisReg1;       
    WORD    AsisReg2;       
    LH_U16  Length;         
    LH_U16  Width;          
    
    CPU_IntDis();
    f_mount(0, &Fs);        
    if(f_opendir(&Dir, "") == FR_OK)                            
        if(f_open(&File, (TCHAR *)filename, FA_READ) == FA_OPEN_EXISTING | FR_OK)	
        {
            CPU_IntEn();
            if(CheckBuffSta() == LH_BUFF_FREE)                  
            {
                SetBuffBusy();          
                do
                {
                    
                    if(Read_Flag == 0)  
                    {
                        CPU_IntDis();
						if(f_read(&File, LH_DataBuff, 24, &ByteR) != FR_OK)
                            break; 
                        CPU_IntEn();
                        
                        if((LH_DataBuff[0] != 0x42) || (LH_DataBuff[1] != 0x4D))
                            break;
                        Length = LH_DataBuff[18] + (LH_DataBuff[19]<<8);    
                        Width  = LH_DataBuff[22] + (LH_DataBuff[23]<<8);    
                        if(Length%2 != 0) 
                            Length ++;
                        


                        if (sx < 0) sx = 0;
                        if (sy < 0) sy = 0;
                        AsisReg1 = sx+Length-1;
                        if (AsisReg1 >= TFT_XSIZE) AsisReg1 = TFT_XSIZE-1;
                        AsisReg2 = sy+Width-1;
                        if (AsisReg2 >= TFT_YSIZE) AsisReg2 = TFT_YSIZE-1;
                        TFT_SetWindows(sx, sy, AsisReg1, AsisReg2);     
                        RA8875_SetCursor(AsisReg1, sy);                 
                        RA8875_REG = 0x02; 		                        
                        
                        CPU_IntDis();
                        
                        f_lseek(&File, LH_DataBuff[10] + (LH_DataBuff[11] << 8)
                                                       + (LH_DataBuff[12] << 16)
                                                       + (LH_DataBuff[13] << 24)); 
                        if(f_read(&File, LH_DataBuff, LH_DATA_BUFF_SIZE, &ByteR) != FR_OK)
                            break; 
                        CPU_IntEn();
                    }
					else
                    {
                        CPU_IntDis();
						if(f_read(&File, LH_DataBuff, LH_DATA_BUFF_SIZE, &ByteR) != FR_OK)
                        	break;
                        CPU_IntEn();
                    }
                    
                    LCD_OperateMut = 1; 
                    
                    for(Count1 = 0; Count1 < ByteR; Count1 += 2)
                    {


                        RA8875_RAM = LH_DataBuff[Count1] + (LH_DataBuff[Count1 + 1] << 8); 
                    }
                    LCD_OperateMut = 0; 
                    Read_Flag = 1;
                }
                while(ByteR != 0);
                TFT_SetWindows(0, 0, 479, 271); 
                SetBuffFree();          
            }
            CPU_IntDis();
            f_close(&File); 
        }
    f_mount(0,NULL);        


    CPU_IntEn();
        
    return LH_OK;
}



LH_U8 LH_ShowBmpSpAt(LH_S16 sx, LH_S16 sy, LH_S16 rsx, LH_S16 rsy, LH_S16 rex, LH_S16 rey, LH_C8 *filename)
{
    FATFS   Fs;             

    FIL     File;           
    DIR     Dir;            
    UINT    ByteR;          
    WORD    i1;             
    WORD    i2;             
    WORD    i3;             
    SHORT   AsisReg1;       
    LONG    Length;         
    WORD    Width;          
    LONG    JmpLeft;        
    SHORT   DrawLength;     
    LONG    JmpDown;        
    SHORT   DrawUpCf;       
    SHORT   DrawWith;       

    CPU_IntDis();
    f_mount(0, &Fs);        
    if(f_opendir(&Dir, "") == FR_OK)                            
        if(f_open(&File, (TCHAR *)filename, FA_OPEN_EXISTING | FA_READ) == FR_OK)	
        {
            if(CheckBuffSta() == LH_BUFF_FREE)                  
            {
                SetBuffBusy();      
                
                if (f_read(&File, LH_DataBuff, 24, &ByteR) == FR_OK)
                { 
                    CPU_IntEn();
                    
                    if((LH_DataBuff[0] == 0x42) && (LH_DataBuff[1] == 0x4D))
                    {
                        Length = LH_DataBuff[18] + (LH_DataBuff[19]<<8);    
                        Width  = LH_DataBuff[22] + (LH_DataBuff[23]<<8);    
                        if (Length%2 != 0) 
                            Length ++;                              
                        
                        JmpLeft = rsx - sx;                         
                        if (JmpLeft < 0) JmpLeft = 0;    
                        AsisReg1 = (sx + Length) - rex;             
                        if (AsisReg1 < 0) AsisReg1 = 0;
                        DrawLength = Length - JmpLeft - AsisReg1;   
                        
                        JmpDown = (sy + Width) - rey;               
                        if (JmpDown < 0) JmpDown = 0;
                        DrawUpCf = rsy - sy;                        
                        if (DrawUpCf < 0) DrawUpCf = 0;
                        DrawWith = Width - JmpDown - DrawUpCf;      
                        
                        if( (DrawLength > 0) && (DrawWith > 0) )    
                        {                   


                            
                            TFT_SetWindows(GetMax(rsx, sx), GetMax(rsy, sy), GetMin(rex, sx+Length), GetMin(rey, sy+Width));     
                                 
                            
                            AsisReg1 = LH_DataBuff[10] + (LH_DataBuff[11] << 8) + (LH_DataBuff[12] << 16) + (LH_DataBuff[13] << 24); 
                            
                            for(i1 = 0; i1 < DrawWith; i1 ++)       
                            {
                                CPU_IntDis();
                                
                                f_lseek(&File, AsisReg1 + 2*((JmpDown + i1)*(Length) + JmpLeft));
                                CPU_IntEn();
                                
                                i2 = 0;
                                while (i2 <= DrawLength)
                                {
                                    CPU_IntDis();
                                    if(f_read(&File, LH_DataBuff, LH_DATA_BUFF_SIZE, &ByteR) != FR_OK)
                                        break; 
                                    CPU_IntEn();
                                
                                    
                                    for(i3 = 0; i3 < ByteR; i3 += 2)
                                    {

                                        i2 ++;  
                                        if(i2 == DrawLength+1)
                                            break;
                                    }
                                }
                            }
                        }
                    }
                }
                SetBuffFree();      
            }
            CPU_IntDis();
            f_close(&File);         
        }

    f_mount(0,NULL);                


    CPU_IntEn();
        
    return LH_OK;
}



LH_U8 GetWidgetLenWid(LH_U16 *length, LH_U16 *width, LH_C8 *filename)
{
    FATFS   Fs;                 

    FIL     File;               
    DIR     Dir;                
    UINT    ByteR;              
    LH_U8   FuncSta = LH_OK;    
    
    CPU_IntDis();
    f_mount(0, &Fs);            
    if(f_opendir(&Dir, "") == FR_OK)                            
        if(f_open(&File, (TCHAR *)filename, FA_OPEN_EXISTING | FA_READ) == FR_OK)  
        {
            if(CheckBuffSta() == LH_BUFF_FREE)                  
            {
                SetBuffBusy();          

                if(f_read(&File, LH_DataBuff, 24, &ByteR) == FR_OK)
                { 
                    *length = LH_DataBuff[18] + (LH_DataBuff[19]<<8);
                    *width  = LH_DataBuff[22] + (LH_DataBuff[23]<<8);
                } 
                else
                    FuncSta = LH_ERROR; 

                SetBuffFree();          
            }
            f_close(&File); 
        }
        else
        {
            *length = 0;
            *width  = 0;
        }
            
    f_mount(0,NULL);        
    CPU_IntEn();   

    return FuncSta;
}



void LH_ShowNum(LH_U16 sx, LH_U16 sy, LH_U16 num)
{
    LH_S8 FileName[] = "GUI/ch/0.bmp";
    LH_C8 *DotFile = "GUI/ch/10.bmp";
    LH_S8 OneNum;
    
    OneNum = num/1000 + '0';
    FileName[7] = OneNum;
    LH_ShowBmpAt(sx, sy, (LH_C8 *)FileName);
    OneNum = num%1000/100 + '0';
    FileName[7] = OneNum;
    LH_ShowBmpAt(sx+13, sy, (LH_C8 *)FileName);
    OneNum = num%100/10 + '0';
    FileName[7] = OneNum;
    LH_ShowBmpAt(sx+26, sy, (LH_C8 *)FileName);
    LH_ShowBmpAt(sx+40, sy, DotFile);
    OneNum = num%10 + '0';
    FileName[7] = OneNum;
    LH_ShowBmpAt(sx+44, sy, (LH_C8 *)FileName);
}



